package ${api};

import ${dispatch}.I${className}Dispatch;
import ${entity}.${className};
import com.open.model.Page;
import com.open.model.PageRequest;
import com.open.model.Result;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;

/**
 * $!codeName
 *
 * @author $!currentUser
 * @date $!date
 */
@RestController
#if(${pathPrefix} && ${pathPrefix} != "")
@RequestMapping("${pathPrefix}/${lowerName}")
#else
@RequestMapping("/${lowerName}")
#end
public class ${className}Api{

    @Autowired
    private I${className}Dispatch ${lowerName}Dispatch;

    /**
     * 对象添加
     * @param model 添加的对象
     * @return result
     */
    @PostMapping("save")
    public Result<Boolean> save(@RequestBody ${className} model) {
        long now = System.currentTimeMillis();
        Result<Boolean> result = new Result<>();
        result.setData(${lowerName}Dispatch.save(model));
        result.setTime(System.currentTimeMillis() - now);
        return result;
    }

    /**
     * 对象修改
     * @param model 修改的对象
     * @return result
     */
    @PostMapping("update")
    public Result<Boolean> update(@RequestBody ${className} model) {
        long now = System.currentTimeMillis();
        Result<Boolean> result = new Result<>();
        result.setData(${lowerName}Dispatch.update(model));
        result.setTime(System.currentTimeMillis() - now);
        return result;
    }

    /**
     * 根据对象列表查询
     * @param model 查询条件
     * @return result
     */
    @PostMapping("findByModel")
    public Result<List<${className}>> findByModel(@RequestBody ${className} model) {
        long now = System.currentTimeMillis();
        Result<List<${className}>> result = new Result<>();
        result.setData(${lowerName}Dispatch.findByModel(model));
        result.setTime(System.currentTimeMillis() - now);
        return result;
    }

    /**
     * 根据主键查询对象
     * @param id 查询的对象主键
     * @return result
     */
    @GetMapping("findByPk")
    public Result<${className}> findByPk(@RequestParam("id") ${pk.jdbcType} id) {
        long now = System.currentTimeMillis();
        Result<${className}> result = new Result<>();
        result.setData(${lowerName}Dispatch.findByPk(id));
        result.setTime(System.currentTimeMillis() - now);
        return result;
    }

    /**
     * 分页查询
     * @param pageRequest 分页查询条件
     * @return result
     */
    @PostMapping("findPage")
    public Result<Page<${className}>> findPage(@RequestBody PageRequest<${className}> pageRequest) {
        long now = System.currentTimeMillis();
        Result<Page<${className}>> result = new Result<>();
        result.setData(${lowerName}Dispatch.findPage(pageRequest.getPage(), pageRequest.getModel()));
        result.setTime(System.currentTimeMillis() - now);
        return result;
    }

    /**
     * 根据主键批量删除对象
     * @param ids 删除的对象主键
     * @return result
     */
    @GetMapping("remove")
    public Result<Boolean> remove(@RequestParam("ids") List<${pk.jdbcType}> ids) {
        long now = System.currentTimeMillis();
        Result<Boolean> result = new Result<>();
        result.setData(${lowerName}Dispatch.remove(ids));
        result.setTime(System.currentTimeMillis() - now);
        return result;
    }

    /**
     * 根据主键删除对象
     * @param id 删除的对象主键
     * @return result
     */
    @GetMapping("delete")
    public Result<Boolean> delete(@RequestParam("id") ${pk.jdbcType} id) {
        long now = System.currentTimeMillis();
        Result<Boolean> result = new Result<>();
        result.setData(${lowerName}Dispatch.delete(id));
        result.setTime(System.currentTimeMillis() - now);
        return result;
    }
}